package net.sf.rose.web.spring.servlet.handler;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

@SuppressWarnings("deprecation")
public class FormConvert {

	public Map<String, Object> convertRequest(HttpServletRequest request) {
		Map<String, Object> map = null;
		if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) {
			map = this.getPostFileBean(request);
		} else {
			Object obj = request.getParameter("code");
			if (obj != null) {
				map = this.getDecodePostBean(request);
			} else {
				map = this.getPostBean(request);
			}
		}
		return map;
	}

	/**
	 * 
	 */
	private Map<String, Object> getPostFileBean(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			DiskFileUpload fu = new DiskFileUpload();
			//
			fu.setSizeMax(4194304);
			//
			fu.setSizeThreshold(4096);
			//
			String tempath = System.getenv("TEMP");
			if (tempath == null) {
				tempath = "c:/";
			}
			fu.setRepositoryPath(tempath);

			fu.setHeaderEncoding(request.getCharacterEncoding());

			//
			Iterator<?> it = fu.parseRequest(request).iterator();
			FileItem item = null;
			String key = null;
			String val = null;
			while (it.hasNext()) {
				item = (FileItem) it.next();
				if (item.isFormField()) {
					key = item.getFieldName();
					val = item.getString("UTF-8");
					if (!val.toString().trim().equals("")) {
						if (key.equals("method")) {
							request.setAttribute("method", val);
						} else {
							map.put(key, val);
						}
					} else {
						map.put(key, null);
					}
				} else {
					if (!item.getName().equals("")) {
						key = item.getFieldName();// 字段名
						String name = item.getName();// 文件名
						name = name.substring(name.lastIndexOf('\\') + 1, name.length());
						map.put(key.replaceFirst("Data", ""), name);// 文件名
						map.put(key, item.getInputStream());// 附件数据
					}
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return map;
	}

	/**
	 * 
	 */
	private Map<String, Object> getPostBean(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Object val = null;
		StringBuffer buf = new StringBuffer();
		Map m = request.getParameterMap();
		for (Object o : m.keySet()) {
			val = m.get(o);
			if (val instanceof String[]) {
				String[] arr = (String[]) val;
				buf = new StringBuffer();
				for (String str : arr) {
					buf.append(',').append(str);
				}
				val = buf.substring(1);
			}
			if (!val.toString().trim().equals("")) {
				map.put(o.toString(), val);
			}
		}
		return map;
	}

	/**
	 * 
	 */
	private Map<String, Object> getDecodePostBean(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String key = null;
			Object val = null;
			Enumeration<?> en = request.getParameterNames();
			while (en.hasMoreElements()) {
				key = en.nextElement().toString();
				val = request.getParameter(key);
				val = java.net.URLDecoder.decode(val.toString(), "UTF-8");
				if (!val.toString().trim().equals("")) {
					map.put(key, val);
				} else {
					map.put(key, null);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return map;
	}

}
